package com.bafa.services;

import com.bafa.dbcache.GrabReports;
import com.bafa.jdbc.DBAccess;
import com.bafa.objects.Message;
import com.bafa.objects.Report;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class ReportServices {
    public ReportServices() {
    }

    public static Message addGeneralArticle(String id,String author, String report, 
                                            String title, String category) {
        Report reportBean = new Report();
        reportBean.setAuthor(author);
        reportBean.setReport(report);
        reportBean.setTitle(title);
        reportBean.setCategory(category);
        reportBean.setReportId(id);
        DBAccess db = new DBAccess();


        try {
            db.deleteReport(id);
            db.storeReport(reportBean);
        } catch (Exception e) {

        }

        GrabReports.addReports(reportBean);
        return new Message();
    }

    public static Message addMainNews(String author, String report, 
                                            String title, String category) {
        Report reportBean = new Report();
        reportBean.setAuthor(author);
        reportBean.setReport(report);
        reportBean.setTitle(title);
        reportBean.setCategory(category);
        reportBean.setReportId("MAINNEWS");
        DBAccess db = new DBAccess();


        try {
            db.deleteReport("MAINNEWS");
            db.storeReport(reportBean);
        } catch (Exception e) {

        }

        GrabReports.addReports(reportBean);
        return new Message();
    }
    
    public static Message addMatchReport(String author, String report, 
                                            String title, String category, int matchId, int teamId, int seasonId) {
        Report reportBean = new Report();
        reportBean.setAuthor(author);
        reportBean.setReport(report);
        reportBean.setTitle(title);
        reportBean.setCategory(category);
        reportBean.setMatchId(matchId);
        reportBean.setTeamId(teamId);
        reportBean.setSeasonId(seasonId);
        reportBean.setReportId(matchId+""+teamId+""+seasonId+"");
        DBAccess db = new DBAccess();


        try {
            db.deleteReport(matchId+""+teamId+""+seasonId+"");
            db.storeReport(reportBean);
        } catch (Exception e) {
            e.printStackTrace();
        }

        GrabReports.addReports(reportBean);
        return new Message();
    }
    
    public static boolean doesReportExistForMatchAndTeam(int matchId, int teamId){
        if(GrabReports.getMatchReports().containsKey(matchId)){
         
            HashMap teamReports = (HashMap)GrabReports.getMatchReports().get(matchId);
            if(teamReports.containsKey(teamId)){
              
                return true;
            }else{
                
                return false;
            }
        }else{
            
            return false;
        }
    }
    
    public static boolean doesReportExistForMatch(int matchId){
        if(GrabReports.getMatchReports().containsKey(matchId)){
                return true;
        }else{
                return false;
        }
       
    }
    
    public static Report getReport(int matchId, int teamId){
        if(GrabReports.getMatchReports().containsKey(matchId)){
            HashMap teamReports = (HashMap)GrabReports.getMatchReports().get(matchId);
            if(teamReports.containsKey(teamId)){
                return (Report)GrabReports.getReports().get(teamReports.get(teamId));
            }else{
                return null;
            }
        }else{
            return null;
        }
    }
    
    public static Report getReport(String id){
        return (Report)GrabReports.getReports().get(id);
    }
    public static Iterator getReport(int matchId){
        if(GrabReports.getMatchReports().containsKey(matchId)){
            HashMap teamReports = (HashMap)GrabReports.getMatchReports().get(matchId);
            
            return teamReports.values().iterator();
        }
        
        return null;
    }
    
    public static ArrayList getSeasonReports(int seasonId){
        return (ArrayList)GrabReports.getSeasonReports().get(seasonId);
    }
    
}
